﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JeanFish.Blog.BLL;
using JeanFish.Blog.BLL.Relationships;
using JeanFish.Blog.Model;

namespace JeanFish.UnitTest.Blog
{
    [TestClass]
    public class CategoryTest
    {
        [TestInitialize]
        public void RandomCategory()
        {
            this.categoryName = "Category:" + Guid.NewGuid().ToString();
            this.categoryDes = "Description:" + categoryName;
            this.postTitle = "Post:" + Guid.NewGuid().ToString();
            this.postText = "Text:" + postTitle;
            this.blogName = "Blog:" + Guid.NewGuid().ToString();
        }
        private string categoryName = "";
        private string categoryDes = "";
        private string postTitle = "";
        private string postText = "";
        private string blogName = "";
        [TestMethod]
        public void BizCategoryTest()
        {
            BizCategory biz = new BizCategory();
            int id = 0;
            Category c = biz.GetCategory(id);
            Assert.IsNull(c);
            c = biz.CreateCategory(categoryName, categoryDes);
            Assert.IsTrue(c.ID > 0);
            Category c2 = biz.GetCategory(c.ID);
            Assert.AreEqual(c.ID, c2.ID);
            Assert.AreEqual(c.Title, c2.Title);
            c.Delete();
            biz.UpdateCategory(c);
            c = biz.GetCategory(c.ID);
            Assert.IsNull(c);
            c = biz.CreateCategory(categoryName, categoryDes);
            Assert.IsNotNull(c);
        }
        [TestMethod]
        public void CategoryPostsTest()
        {
            BlogCategory bc = new BlogCategory(new BlogInfo(blogName));
            bc.AddValue(new Category(categoryName));
            bc.AddValue(new Category(categoryName + "1"));
            bc.Save();
            PostCategory pc = new PostCategory(new Post(postTitle, postText));
            pc.AddValue(bc.List());
            pc.RemoveValue(t => t.Title == categoryName);
            pc.Save();
            Assert.AreEqual(1, pc.Count);
            CategoryPosts cp = new CategoryPosts(pc.List(t => t.Title == categoryName + "1")[0]);
            Assert.AreEqual(1, cp.Count);
        }
    }
}
